import com.sun.org.apache.xpath.internal.objects.XString;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-06
 * Time: 18:48
 */
//数组2
public class Test {

    /*
    给定一个整型数组, 判定数组是否有序（递增）
     */
    public static void main16(String[] args) {
        int[] array = {1,3,2,4};
        boolean change = true;
        for (int i = 1; i <array.length ; i++) {
            if(array[i-1] > array[i]){
                change = false ;
            }
        }
        System.out.println(change);
    }
    /*
    两数之和
     */
    public static void main15(String[] args) {
        int[] array = {2,7,11,15};
        int target = 9;
        int[] ret = new int[2];
        for (int i = 0; i < array.length ; i++) {
            for (int j = i+1; j < array.length ; j++) {
                if(array[i] + array[j] == target){
                    ret[0] = i;
                    ret[1] = j;
                }
            }
        }
        System.out.println(Arrays.toString(ret));
    }
    /*
    创建一个 int 类型的数组, 元素个数为 100, 并把每个元素依次设置为 1 - 100
     */
    public static void main14(String[] args) {
        int[] array = new int[101];
        for (int i = 0; i <101; i++) {
            array[i] = i;
        }
        System.out.println(Arrays.toString(array));
    }
    /*
    实现一个方法 transform, 以数组为参数,
    循环将数组中的每个元素 乘以 2 , 并设置到对应的数组元素上.
    例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}
     */
    public static int[] transform(int[] array){
        for (int i = 0; i <array.length ; i++) {
            array[i] *= 2;
        }
        return array;
    }
    public static void main13(String[] args) {
        int[] array = {1,2,3};
        int[] ret = new int[array.length];
        ret = transform(array);
        System.out.println(Arrays.toString(array));
    }
/*
tostring  数组拷贝 求数组中元素的平均值  查找数组中的指定元素  数组排序  数组逆置
 */
    public static  void test(int [] tmp){
        tmp = new int[]{11,22,33};
    }
    public static void reverse(int[] array){
        int left = 0;
        int right = array.length-1;
        while(left < right){
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
        }
    }
    public static void main12(String[] args) {
        int[] array = {1,2,3,4,5};
        reverse(array);
        System.out.println(Arrays.toString(array));
    }
    public static int find(int[] array,int x){
        for (int i = 0; i <array.length ; i++) {
            if(array[i] == x){
                return i;
            }
        }
        return -1;
    }
    public static void main11(String[] args) {
        int [] array = {1,2,3,4,5};
        int ret = find(array,3);
        System.out.println(ret);
    }
    public static int avg(int [] arry){
        int sum = 0;
        for (int i = 0; i < arry.length ; i++) {
            sum += arry[i];
        }
        return sum;
    }
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean change = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    change = true;
                }
            }
            if(change == false){
                return ;
            }
        }
    }
    public static void main10(String[] args) {
        int [] array = {1,2,3,13,4,5};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main9(String[] args) {
        int [] arry ={1,2,3,4,5,6};
        int ret = avg(arry);
        System.out.println(ret*1.0/arry.length);
    }
    public static void main8(String[] args) {
        int [] arry = {1,2,3,4,5};
        int [] copy = new int[arry.length];
    }
    public static void main7(String[] args) {
        int [] arry = {1,2,3,4,5};
        int [] copy = Arrays.copyOf(arry,arry.length);
        System.out.println("------------------------");
        System.out.println(Arrays.toString(arry));
        System.out.println(Arrays.toString(copy));
    }
    public static void copyOf(int[] arry,int[] copy){
        for (int i = 0; i < arry.length; i++) {
            copy[i] = arry[i];
        }
    }
    public static void main5(String[] args) {
        int [] arry = {1,2,3,4,5};
        int [] copy = new int[arry.length] ;
        copyOf(arry,copy);
        System.out.println("------------------------");
        System.out.println(Arrays.toString(arry));
        System.out.println(Arrays.toString(copy));
    }
    public static void my_tostring(int [] arry){
        for (int i = 0; i <arry.length ; i++) {
            System.out.print(arry[i]+" ");
        }
    }
    public static void main4(String[] args) {
        int arry[]= {1,2,3,4,5};
        my_tostring(arry);
    }
    public static void main3(String[] args) {
        int arry[] = new int[]{1,2,3,4};
        int copy[] = arry;
        test(arry);
        System.out.println(Arrays.toString(arry));
        System.out.println(Arrays.toString(copy));
    }
    public static void main2(String[] args) {
        int arry[] = new int[]{1,2,3,4};
        for (int i = 0; i <arry.length ; i++) {
            System.out.print(i+" ");
        }
        System.out.println();
    }
    public static void main1(String[] args) {
        int arry[] = {1 ,2 ,3 ,4};
        test(arry);
        String ret = Arrays.toString(arry);
        System.out.println(ret);
    }
}
